package com.netty.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netty.common.entity.result.Result;
import org.springframework.http.HttpStatus;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** 输出工具类
 * @author LiuXiaoYu
 */
public class ResponseUtils {

    public static void out(HttpServletResponse response, Result<?> r) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json;charset=UTF-8");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            mapper.writeValue(outputStream, r);
            outputStream.flush();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
